import React, { Component } from 'react';
import { connect } from 'dva';
import { message, Button } from 'antd';
import styles from './../Users.css';
import Factory from '../../factory';
import * as datetime from '../../../utils/datetime'
import dataTableCreate from '../../addons/DataTable'
const daoName = "FlowDoingSum";
const ButtonGroup = Button.Group;
const columns = [
    { title: '事项', width: 160, dataIndex: 'WFNM', key: 'WFNM', dispaly: true },
    { title: '任务', width: 160, key: 'StepName', dataIndex: 'StepName', dispaly: true },
    { title: '开始时间', width: 140, dataIndex: 'StartTime', key: 'StartTime', render: StartTime => datetime.format(StartTime, "yyyy-MM-dd"), dispaly: true },
];
const DataTable = dataTableCreate(daoName);
class FlowDoingSumForm extends Component {
    constructor(props) {
        super(props);
        this.state = {
            dataChange: false
        };
    }
    componentDidMount() {
        this.props.dispatch({
            type: `${daoName}/setKeyName`,
            payload: { keyName: "ID", permID: this.props.permID }
        });
        let whereItems = [{
            FieldName: "EMID",
            Operator: "=",
            FieldValues: '' + this.props.userID
        }];
        const seeks = [{
            WhereItems: whereItems,
            RelationSign: "AND"
        }];
        this.props.dispatch({
            type: `${daoName}/fetch`,
            payload: { daoName, page: 1, pageSize: 20, seeks }
        });
    }
    doHandler() {
        const keys = this.props.selectedRowKeys;
        const len = keys.length;
        if (len === 0) {
            message.info('请选择要办理的任务！',5);
            return;
        }
        const key = keys[0]
        const { list, menus, idIndexMap } = this.props
        const row = list.find(r => r.ID === key)
        const ind = idIndexMap[row.PermID];
        const m = menus[ind]
        this.toDoflow(m,ind)
    }
    toDoflow(m, index) {
        let procsStr = `{"content":"` + m.Name + `正在建设中..."}`
        let pName = "AbuildingForm"
        if (m.ReactComponent && m.ReactComponent !== "") {
            pName = m.ReactComponent
            if (m.ReactPorps && m.ReactPorps !== "") {
                procsStr = m.ReactPorps
            } else {
                procsStr = "{}"
            }
        }
        let proc = JSON.parse(procsStr)
        proc.permID = m.ID
        this.props.dispatch({
            type: 'menus/addPane',
            payload: { id: m.ID, name: m.Name, comp: pName, props: proc, index:''+index  }
        });
    }
    render() {
        let { showEdit, record, permID, popedomTypes } = this.props
        const pTypes = popedomTypes[permID]
        let data = null
        if (pTypes.indexOf("2") !== -1) {
            data = <ButtonGroup style={{ zIndex: 0 }}>
                <Button onClick={this.doHandler.bind(this)}>办理</Button>
            </ButtonGroup>
        }
        return (
            <div className={styles.normal} style={{ background: '#fff', marginTop: -16, padding: '5px 5px' }}>
                <div>
                    <div className={styles.create}>
                        {data}
                    </div>
                    <DataTable ref="FlowDoingSumTable" daoName={daoName} columns={columns} permID={permID} />
                </div>
            </div>
        )
    }
}
function mapStateToProps(state) {
    const { showEdit, record, selectedRowKeys, keyName, list } = state[daoName];
    const { userID, orgID, pOrgID, popedomTypes, menus, idIndexMap, parents } = state.menus;
    return {
        userID,
        orgID,
        pOrgID,
        popedomTypes,
        showEdit,
        selectedRowKeys,
        keyName,
        record,
        list, menus, idIndexMap, parents
    }
}
FlowDoingSumForm = connect(mapStateToProps)(FlowDoingSumForm)
export default FlowDoingSumForm;

let render = (props) => React.createElement(FlowDoingSumForm, props);
let f = new Factory()
f.set("FlowDoingSumForm", render)
